package com.ygl.strategy.service.opentracing.tracer;

import com.ygl.strategy.opentracing.operation.StrategyOpentracingOperation;
import com.ygl.strategy.service.tracer.DefaultServiceStrategyTracer;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.HashMap;
import java.util.Map;

/**
 * author: yanggld
 * date: 2019/12/22 19:36
 * version: 1.0
 * description: DefaultServiceStrategyOpentracingTracer
 */
public class DefaultServiceStrategyOpentracingTracer extends DefaultServiceStrategyTracer {

    @Autowired
    private StrategyOpentracingOperation strategyOpentracingOperation;

    @Override
    public void trace(HandlerMethod handlerMethod) {
        strategyOpentracingOperation.opentracingInitialize();

        super.trace(handlerMethod);

        strategyOpentracingOperation.opentracingLocal(handlerMethod.getBean().getClass().toString(), handlerMethod.getMethod().getName(), getCustomizationMap());

    }

    private Map<String, String> getCustomizationMap() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "ygl");
        return map;
    }


    @Override
    public void error(HandlerMethod handlerMethod, Throwable e) {
        super.error(handlerMethod, e);
        strategyOpentracingOperation.opentracingError(handlerMethod.getBean().getClass().toString(), handlerMethod.getMethod().getName(), e);
    }

    @Override
    public void release(HandlerMethod handlerMethod) {
        super.release(handlerMethod);
        strategyOpentracingOperation.opentracingClear();
    }

    public String getTraceId() {
        return strategyOpentracingOperation.getTraceId();
    }

    public String getSpanId() {
        return strategyOpentracingOperation.getSpanId();
    }
}
